package hu.home.arb.net;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.IncorrectnessListener;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.SilentCssErrorHandler;
import com.gargoylesoftware.htmlunit.WebClient;

public class Net {
	
	//public static WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17, "hqnlb.iroda.educatio.hu", 8080);
	public static WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
	
	public static WebClient getWebClient() {
		
		//DefaultCredentialsProvider credentialsProvider = new DefaultCredentialsProvider();
		//credentialsProvider.addNTLMCredentials("tothg", "Thnzjm789", "hqnlb.iroda.educatio.hu", 8080, "10.6.11.81", "educatio");
		//webClient.setCredentialsProvider(credentialsProvider);
		
		return webClient;
	}
	
	public Net() {
		super();
		
		//ProxyConfig proxyConfig = new ProxyConfig("127.0.0.1", 8888);
		//webClient.getOptions().setProxyConfig(proxyConfig);
		
		webClient.getOptions().setJavaScriptEnabled(true);
		webClient.getOptions().setThrowExceptionOnScriptError(true);
		webClient.setAjaxController(new NicelyResynchronizingAjaxController()); 
		
		webClient.getOptions().setTimeout(60000);
		webClient.setJavaScriptTimeout(30000);
	    webClient.getOptions().setRedirectEnabled(true);
	    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
	    webClient.getOptions().setThrowExceptionOnScriptError(false);
	    webClient.getOptions().setCssEnabled(false);
	    webClient.getOptions().setUseInsecureSSL(true);
	    
	    final IncorrectnessListener il = new IncorrectnessListener() {			
			public void notify(String arg0, Object arg1) {
				
			}
		};
        webClient.setIncorrectnessListener(il); 
        final SilentCssErrorHandler eh = new SilentCssErrorHandler();
        webClient.setCssErrorHandler(eh);
		
		/*
		webClient.setAjaxController(new AjaxController(){
		    @Override
		    public boolean processSynchron(HtmlPage page, WebRequest request, boolean async)
		    {
		        return true;
		    }
		});
		*/
	}
	
	public String getSerializedWebclient(WebClient webClient) {
		String ret = null;
		
		FileOutputStream fileOut = null;
		try {
			fileOut = new FileOutputStream("webclient.ser");
	        ObjectOutputStream out = new ObjectOutputStream(fileOut);
	        out.writeObject(webClient);
	        out.close();
	        fileOut.close();
	    } catch(IOException i) {
	    	i.printStackTrace();
	    }
				
		return ret;
	}
	
	public WebClient getWebclientFromSerializedStr(String strWebClient) {
		WebClient webClient = null;
		
		try {
			FileInputStream f_in = new FileInputStream("myobject.data");
			ObjectInputStream obj_in = new ObjectInputStream (f_in);
			webClient = (WebClient) obj_in.readObject();
			obj_in.close();
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		
		return webClient;
	}
	
}
